package com.business.dashboard.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.business.dashboard.entity.NewsArticle;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 新闻文章数据访问层
 * 
 * @author Claude
 * @since 2025-07-06
 */
@Mapper
public interface NewsArticleDao extends BaseMapper<NewsArticle> {

    /**
     * 查询文章列表（分页）
     */
    @Select("<script>" +
            "SELECT * FROM news_articles WHERE deleted = 0 " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "AND (title LIKE CONCAT('%', #{keyword}, '%') OR summary LIKE CONCAT('%', #{keyword}, '%')) " +
            "</if>" +
            "<if test='status != null and status != \"\"'>" +
            "AND status = #{status} " +
            "</if>" +
            "<if test='categoryId != null'>" +
            "AND category_id = #{categoryId} " +
            "</if>" +
            "ORDER BY created_at DESC LIMIT #{offset}, #{size}" +
            "</script>")
    List<NewsArticle> selectArticleList(@Param("offset") int offset, 
                                       @Param("size") int size, 
                                       @Param("keyword") String keyword,
                                       @Param("status") String status,
                                       @Param("categoryId") Long categoryId);

    /**
     * 根据状态统计文章数量
     */
    @Select("SELECT COUNT(*) FROM news_articles WHERE status = #{status} AND deleted = 0")
    int countByStatus(@Param("status") String status);

    /**
     * 更新文章浏览次数
     */
    @Update("UPDATE news_articles SET view_count = view_count + 1 WHERE id = #{id}")
    int updateViewCount(@Param("id") Long id);

    /**
     * 查询已发布的新闻列表（用户端）
     */
    @Select("<script>" +
            "SELECT * FROM news_articles WHERE status = 'published' AND deleted = 0 " +
            "<if test='category != null and category != \"\"'>" +
            "AND category = #{category} " +
            "</if>" +
            "ORDER BY created_at DESC LIMIT #{offset}, #{limit}" +
            "</script>")
    List<NewsArticle> selectPublishedNews(@Param("offset") int offset, 
                                         @Param("limit") int limit,
                                         @Param("category") String category);

    /**
     * 统计已发布新闻数量（用户端）
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM news_articles WHERE status = 'published' AND deleted = 0 " +
            "<if test='category != null and category != \"\"'>" +
            "AND category = #{category} " +
            "</if>" +
            "</script>")
    Long countPublishedNews(@Param("category") String category);

    /**
     * 查询热门新闻（按浏览量排序）
     */
    @Select("SELECT * FROM news_articles WHERE status = 'published' AND deleted = 0 " +
            "ORDER BY view_count DESC, created_at DESC LIMIT #{limit}")
    List<NewsArticle> selectHotNews(@Param("limit") int limit);

    /**
     * 查询最新新闻（按创建时间排序）
     */
    @Select("SELECT * FROM news_articles WHERE status = 'published' AND deleted = 0 " +
            "ORDER BY created_at DESC LIMIT #{limit}")
    List<NewsArticle> selectLatestNews(@Param("limit") int limit);

    /**
     * 搜索新闻（按标题和摘要）
     */
    @Select("SELECT * FROM news_articles WHERE status = 'published' AND deleted = 0 " +
            "AND (title LIKE CONCAT('%', #{keyword}, '%') OR summary LIKE CONCAT('%', #{keyword}, '%')) " +
            "ORDER BY created_at DESC LIMIT #{offset}, #{limit}")
    List<NewsArticle> searchNews(@Param("keyword") String keyword,
                                @Param("offset") int offset,
                                @Param("limit") int limit);
}